package com.lune.job.controller;


import com.lune.job.framework.redis.RedisUtil;
import com.lune.job.framework.role.RequireRoles;
import com.lune.job.framework.role.Role;
import com.lune.job.pojo.ResPageBean;
import com.lune.job.pojo.RespBean;
import com.lune.job.pojo.Student;
import com.lune.job.service.IStudentService;
import com.lune.job.utils.UserThreadLocal;
import com.lune.job.vo.UserData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Caiyilin
 * @since 2022-03-06
 */
@RestController
@RequestMapping("/student")
@Api(tags = "学生用户管理")
public class StudentController {

    @Autowired
    private IStudentService studentService;


    @Autowired
    private RedisUtil redisUtil;


    @ApiOperation(value = "添加学生用户")
    @PostMapping("/")
    public RespBean create(@RequestBody Student student) {
        boolean save = studentService.save(student);

        if (studentService.save(student)) {
            return RespBean.success("添加学生成功");
        } else {
            return RespBean.error("添加学生失败");
        }
    }

    @ApiOperation(value = "删除学生")
    @DeleteMapping("/{id}")
    public RespBean delete(@PathVariable Integer id) {
        if (studentService.removeById(id)) {
            return RespBean.success("删除学生成功");
        } else {
            return RespBean.error("删除学生失败");
        }
    }

    @ApiOperation(value = "批量删除学生")
    @DeleteMapping("/")
    public RespBean deleteByIds(Integer[] ids) {
        if (studentService.removeByIds(Arrays.asList(ids))) {
            return RespBean.success("批量删除学生成功");
        } else {
            return RespBean.error("批量删除学生失败");
        }
    }

    @ApiOperation(value = "根据id查询学生")
    @GetMapping("/detail")
    public RespBean detail(Integer id) {
        Student student = studentService.detail(id);
        if (student.getAccount() != "") {
            return RespBean.success("查询学生成功", student);
        } else {
            return RespBean.error("查询学生失败");
        }

    }

    @ApiOperation(value = "统计学生的数量")
    @GetMapping("/count")
    public int count() {
        return studentService.count();
    }


    @ApiOperation(value = "分页查询所有学生")
    @GetMapping("/queryPage")
    public ResPageBean getAllStudentsByPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                            @RequestParam(defaultValue = "10") Integer size,
                                            Student student) {
        return studentService.getAllStudentsByPage(currentPage, size, student);
    }

    @ApiOperation(value = "获取详细信息")
    @GetMapping("/info")
    @RequireRoles(type = Role.STUDENT)
    public RespBean info() {
        String token = UserThreadLocal.get();
        UserData userData = (UserData) redisUtil.get(token);
        Student detail = studentService.detail(userData.getId());
        return RespBean.success("查询学生信息成功", detail);

    }

    @ApiOperation(value = "条件查询学生用户")
    @PostMapping("/queryByCon")
    public List<Student> queryByCon(@RequestBody Student student) {
        return studentService.queryByCon(student);
    }


}
